package cn.wind.xboot.upload;

/**
 * Created by WangHao on 2019-02-18.
 */
import cn.wind.xboot.core.res.ApiResult;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by xukk on 2017/8/21.
 */
public class OssUpload {

    //    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//    private static String accessKeyId = "LTAIneCYh7IJ6Pq3";
//    private static String accessKeySecret = "mmJMiBKzmQGvAYTgcUQib7sN7TJbmg";
//    private static String bucketName = "v5yundong";
//    private static String key = "";
    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAIUg3wkXF7q4Bf";
    private static String accessKeySecret = "6jDimXrKDg73moXvDoFlZCbnv5jldt";
    private static String bucketName = "wind-qixiang";
    private static String key = "";

    public static void main(String[] args) throws Exception {
        upload();
    }

    public static ApiResult upload()           throws IOException {
        /*
         * Constructs a client instance with your account for accessing OSS
         */
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
            return ApiResult.Custom().success(build(key));
        } catch (OSSException oe) {
            return ApiResult.Custom().failure(oe.getMessage());
        } catch (ClientException ce) {
            return ApiResult.Custom().failure(ce.getMessage());
        } finally {
            client.shutdown();
        }
    }

    public static ApiResult upload(File file, String key) throws IOException {
        /*
         * Constructs a client instance with your account for accessing OSS
         */
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            client.putObject(new PutObjectRequest(bucketName, key, file));
            return ApiResult.Custom().success(build(key));
        } catch (OSSException oe) {
            return ApiResult.Custom().failure(oe.getMessage());
        } catch (ClientException ce) {
            return ApiResult.Custom().failure(ce.getMessage());
        } finally {
            /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
            client.shutdown();
        }
    }

    public static ApiResult upload(InputStream inputStream, String key) throws IOException {
        /*
         * Constructs a client instance with your account for accessing OSS
         */
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            client.putObject(new PutObjectRequest(bucketName, key, inputStream));
            return ApiResult.Custom().success(build(key));
        } catch (OSSException oe) {
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
        } finally {
            client.shutdown();
        }
        return ApiResult.Custom().success();
    }

    private static File createSampleFile() throws IOException {
        File file = File.createTempFile("E://fds", ".jpg");
        file.deleteOnExit();
        return file;
    }

    public static String build(String key) {
        String[] point = endpoint.split("//");
        String src = bucketName + "." + point[1] + "/" + key;
        return src;
    }

    public static String uploadFile(MultipartFile file, String code) throws Exception {
        String imgFileName = file.getOriginalFilename();
        String prefix = imgFileName.substring(imgFileName.lastIndexOf(".") + 1);
        // 生成uuid作为文件名称
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        ApiResult apiRes = upload(file.getInputStream(), code + "/" + code + "_" + uuid + "." + prefix);
        if (apiRes.check()) {
            String img = "https://" + apiRes.getMessage();
            return img;
        } else {
            return null;
        }
    }
}
